<?php
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/user/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/tucao/inc.php");
error_reporting(E_ERROR);


//所有的参数
$g_cgival 	= array();
//存储中间数据 非输出数据 可以不用
$g_pro	 	= array();
//要显示的数据 输出数据
$g_show 	= array();

function check_cgi_pro()
{		
	global $g_cgival, $g_pro, $g_show, $config;
	
	cgi::both($g_cgival["p"], "p", "");
	$g_cgival["p"] = intval($g_cgival["p"]);
	
	if($g_cgival["p"] < 1) $g_cgival["p"]=1;
}


function get_data()
{
	global $g_cgival, $g_pro, $g_show, $config;
	
	$db_user_info = new db_user_info();
	$user_info = new user_info();
	$is_login = $user_info->is_logined();
	
	if($is_login)
	{
		$user_detail_info = $user_info->get_cur_user_info();
		
		$uid = $user_detail_info["uid"];
		$user_nickname = $user_detail_info["user_nickname"];
		$g_show["current_user_info"] = $user_detail_info;
		$g_show["is_logined"] = true;
	}
	else
	{
		$g_show["is_logined"] = false;
		$url = "/user/login.php?url=/message/";
		header("Location:$url");
		exit;
	}
	
	$pcount = 20;
	$type=1;
	$p = $g_cgival["p"];
	
	$db_at_info = new db_at_info();
	$count = $db_at_info->get_count_by_uid($uid);
	$list = $db_at_info->get_list_by_uid($uid,$p,$pcount);
	
	
	$uids = array();
	$at_type_ids = array();
	$max_id =0;
	foreach($list as $key => $value)
	{
		if($max_id < $value["id"])
		{
			$max_id =  $value["id"];
		}
		$uids[] = $value["from_uid"];
		$at_type_ids[$value["at_type"]][] = $value["from_id"];
	}
	$read_at_id = $user_detail_info["read_at_id"];
	
	if($read_at_id < $max_id)
	{
		
		$db_user_info->update_user_info_by_uid($uid,$max_id);
	}
	
	
	$db_tucao = new db_tucao();
	$at_data_array = array();
	foreach($at_type_ids as $key => $value)
	{
		if($key == 1)
		{
			$at_list = $db_tucao->get_tucao_by_ids($value);
			foreach( $at_list as $value)
			{
				$at_data_array[$key][$value["id"]] = $value;
				
			}
			
		}
		
	}
	
	//用户信息
	$user_list = $db_user_info->get_user_list_by_uids($uids);
	$uid_array = array();
	foreach($user_list as $key => $value)
	{
		$uid_array[$value["uid"]] = $value; 
		
	}
	
	//赋值
	foreach($list as $key => $value)
	{
		$list[$key]["detail_data"] = $at_data_array[$value["at_type"]][$value["from_id"]];
		$list[$key]["user_info"] = $uid_array[$value["from_uid"]];
	
	}
	
	$g_show["count"] = $count;
	$g_show["list"] = $list;
	
	
	// 分页
	$page_count = page_bar::get_page_max($g_show["count"], $pcount);
	$page_bar_style = array(
		"curt"		=> $p,
		"max"		=> $page_count,
		"key"		=> "p",
		);
		
	$g_show["page_bar"] = "<b>{$g_show["count"]}</b> 条记录&nbsp;&nbsp;第{$g_cgival["p"]}页&nbsp;共{$page_count}页&nbsp;" . page_bar::bar2($page_bar_style);
	
	
	$g_show["page_title"] ="@你丫的！";
}

try {
	$g_show['error'] = 0;
	$g_show['errmsg'] = "";
	check_cgi_pro();
	get_data();
	
} catch (Exception $e) {
	$g_show['error'] = 1;
	$g_show['errmsg'] = $e->getMessage();
}

show_pro();


exit;

function show_pro()
{
	global $g_cgival, $g_pro, $g_show, $config;

	$smarty = p_get_smarty();	
	$smarty->assign("g_show", $g_show);
	if (isset($_GET["debug"]))
	{
		print_r($g_show);
	}
	$smarty->display("message/index.html");
	
	unset($g_cgival);
	unset($g_pro);
	unset($g_show);
	unset($config);
	
}
?>
